package com.yanze.schoolinformation.adapter.rv;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.view.ViewGroup;

import com.yanze.schoolinformation.R;
import com.yanze.schoolinformation.app.C;
import com.yanze.schoolinformation.base.baseadapter.BaseRecyclerViewAdapter;
import com.yanze.schoolinformation.base.baseadapter.BaseRecyclerViewHolder;
import com.yanze.schoolinformation.databinding.ItemDynamicBinding;
import com.yanze.schoolinformation.databinding.ItemImageBinding;
import com.yanze.schoolinformation.model.bean.Dynamic;
import com.yanze.schoolinformation.utils.PerfectClickListener;
import com.yanze.schoolinformation.view.viewbigimage.ViewBigImageActivity;

import java.util.ArrayList;

/**
 * Created by MrZheng on 2018/5/8.
 */

public class DynamicAdapter extends BaseRecyclerViewAdapter<Dynamic> {

    Context mContext;

    public DynamicAdapter(Context context) {
        this.mContext = context;
    }

    @NonNull
    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(parent, R.layout.item_dynamic);
    }

    private class MyViewHolder extends BaseRecyclerViewHolder<Dynamic, ItemDynamicBinding> {

        public MyViewHolder(ViewGroup viewGroup, int layoutId) {
            super(viewGroup, layoutId);
        }

        @Override
        public void onBindViewHolder(final Dynamic dynamic, int position) {
            binding.setDynamic(dynamic);
            binding.executePendingBindings();
            binding.ivHeader.setOnClickListener(new PerfectClickListener() {
                @Override
                protected void onNoDoubleClick(View v) {
                    ViewBigImageActivity.start(mContext,C.url.API_IMAGE + dynamic.getHeader(),dynamic.getHeader());
                }
            });

            //设置网上获取的图片集合
            if (dynamic.getImages() != null && !dynamic.getImages().isEmpty()) {
                int spanCount = 3;
                if (dynamic.getImages().size() % 2 == 0) {
                    spanCount = 2;
                }
                if (dynamic.getImages().size() % 3 == 0) {
                    spanCount = 3;
                }
                //使用adapter显示
                binding.rvImage.setLayoutManager(new GridLayoutManager(mContext, spanCount));
                ImageAdapter imageAdapter = new ImageAdapter(mContext);
                imageAdapter.addAll(dynamic.getImages());
                binding.rvImage.setAdapter(imageAdapter);
                // 需加，不然滑动不流畅
                binding.rvImage.setNestedScrollingEnabled(false);
                binding.rvImage.setHasFixedSize(false);
                binding.rvImage.setItemAnimator(new DefaultItemAnimator());
            }
        }
    }


    public static class ImageAdapter extends BaseRecyclerViewAdapter<String> {
        private Context mContext;

        public ImageAdapter(Context context) {
            this.mContext = context;
        }

        @NonNull
        @Override
        public BaseRecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ViewHolder(parent, R.layout.item_image);
        }

        class ViewHolder extends BaseRecyclerViewHolder<String, ItemImageBinding> {

            public ViewHolder(ViewGroup viewGroup, int layoutId) {
                super(viewGroup, layoutId);
            }

            @Override
            public void onBindViewHolder(String image, final int position) {
                binding.setImage(image);
                binding.executePendingBindings();
                binding.ivImage.setOnClickListener(new PerfectClickListener() {
                    @Override
                    protected void onNoDoubleClick(View v) {
                        ArrayList<String> images = new ArrayList<>();
                        for (int i = 0; i < data.size(); i++) {
                            images.add(C.url.API_IMAGE + data.get(i));
                        }
                        ViewBigImageActivity.startImageList(mContext, position, images, (ArrayList<String>) data);
                    }
                });
            }
        }
    }
}
